﻿#include "InstructionLoop.h"

#include "ScriptRunner.h"
#include "TurnTableCodec.h"

__SCRIPTINSTRUCTION_REGISTER(InstructionLoop,ScriptInstruction::MetaInstruction_Loop,&InstructionLoop::fromByteArray1);

InstructionLoop::InstructionLoop()
{
  instructionFlag = MetaInstruction_Loop;
}

InstructionLoop::InstructionLoop(InstructionLoop const& instructionLoop)
{
  *this=instructionLoop;
}

InstructionLoop& InstructionLoop::operator=(InstructionLoop const & instructionLoop)
{
  *(ScriptInstruction*)this=instructionLoop;
  loopCount=instructionLoop.loopCount;
  instructionCount=instructionLoop.instructionCount;
  return *this;
}

InstructionLoop::~InstructionLoop()
{}


void InstructionLoop::run()
{
  scriptRunner->newLoop(deviceId,loopCount,instructionCount);
  scriptRunner->scriptWaitAndRun(scriptRunner->getInterval_instruction(),deviceId);
}

QString InstructionLoop::toString()
{
  return QStringLiteral("循环，次数：%1").arg(loopCount);
}

QByteArray InstructionLoop::toByteArray()
{
  return QByteArray().append((char*)&loopCount,sizeof (loopCount))
                     .append((char*)&instructionCount,sizeof (instructionCount));
}

ScriptInstruction* InstructionLoop::fromByteArray1(char * const src,int size,int * offset)
{
  if((size-*offset)<byteArraySize)
    {
      return Q_NULLPTR;
    }
  InstructionLoop* instructionLoop=new InstructionLoop();
  instructionLoop->loopCount=*(int*)(src+*offset);
  offset+=sizeof(loopCount);
  instructionLoop->instructionCount=*(int*)(src+*offset);
  offset+=sizeof(instructionCount);
  return instructionLoop;
}

uint InstructionLoop::getLoopCount()
{
  return loopCount;
}
InstructionLoop* InstructionLoop::setLoopCount(int loopCount)
{
  this->loopCount=loopCount;
  return this;
}
uint InstructionLoop::getInstructionCount()
{
  return instructionCount;
}
InstructionLoop* InstructionLoop::setInstructionCount(int instructionCount)
{
  this->instructionCount=instructionCount;
  return this;
}
